using MediatR;
using Medical.Domain;
using Medical.Infrastructrue.Interfaces;
using Medical.ReadApi.Dto;
using Medical.WriteApi.Appcontext.Command;
using Yitter.IdGenerator;

namespace Medical.WriteApi.Appcontext.CommandHandler
{
	/// <summary>
	/// 创建挂号单模板命令处理器
	/// </summary>
	public class CreateRegistrationSlipTemplateCommandHandler : IRequestHandler<CreateRegistrationSlipTemplateCommand, RegistrationSlipTemplateDto>
	{
		private readonly IBaseRepository<RegistrationSlipTemplate> _repository;

		public CreateRegistrationSlipTemplateCommandHandler(IBaseRepository<RegistrationSlipTemplate> repository)
		{
			_repository = repository;
		}

		public async Task<RegistrationSlipTemplateDto> Handle(CreateRegistrationSlipTemplateCommand request, CancellationToken cancellationToken)
		{
			var entity = new RegistrationSlipTemplate
			{
				TemplateId = YitIdHelper.NextId(),
				TemplateName = request.TemplateName,
				Description = request.Description,
				IsActive = request.IsActive,
				IsDefault = request.IsDefault,
				PriorityRules = request.PriorityRules,
				ConfiguredFields = request.ConfiguredFields,
				CreatedAt = DateTime.Now,
				UpdatedAt = DateTime.Now
			};

			await _repository.InsertAsync(entity);

			return new RegistrationSlipTemplateDto
			{
				TemplateId = entity.TemplateId,
				TemplateName = entity.TemplateName,
				Description = entity.Description,
				IsActive = entity.IsActive,
				IsDefault = entity.IsDefault,
				PriorityRules = entity.PriorityRules,
				ConfiguredFields = entity.ConfiguredFields,
				CreatedAt = entity.CreatedAt,
				UpdatedAt = entity.UpdatedAt
			};
		}
	}
}
